package com.meishij.adapter;

import android.content.Context;
import android.media.MediaPlayer;
import android.net.Uri;
import android.support.v7.widget.RecyclerView;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.TextUtils;
import android.text.style.StrikethroughSpan;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.SurfaceView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.MediaController;
import android.widget.ProgressBar;
import android.widget.RatingBar;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.VideoView;

import com.facebook.drawee.view.SimpleDraweeView;
import com.jash.myutils.utils.ViewUtils;
import com.meishij.R;
import com.meishij.customerview.cityindex.Find_Video_View;
import com.meishij.entry.FindEntry;

import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

/**
 * Created by 阿飞 on 2016/10/12.
 */

public class FindMainRecyclerAdapter extends RecyclerView.Adapter<FindMainRecyclerAdapter.ViewHolder> {

    private Context context;
    private List<FindEntry.DiscoveryItemEntry> list;
    private RecyclerView recyclerView;

    public FindMainRecyclerAdapter(Context context, List<FindEntry.DiscoveryItemEntry> list) {
        this.context = context;
        this.list = list;
        if (list == null) {
            this.list = new ArrayList<>();
        }
    }

    @Override
    public int getItemViewType(int position) {
        FindEntry.DiscoveryItemEntry item = list.get(position);
        String type = item.getType();
        switch (type) {
            case "1":
                return 1;
            case "2":
                return 2;
            case "3":
                return 3;
            case "4":
                return 4;
            case "5":
                return 5;
            case "6":
                return 6;
            case "7":
                return 7;
            case "8":
                return 8;
            case "9":
                return 9;
            case "10":
                return 10;
            case "11":
                return 11;
            case "12":
                return 12;
            case "13":
                return 13;
            case "14":
                return 14;
            case "15":
                return 15;
            case "16":
                return 16;
            case "0":
            default:
                return 0;
        }
    }

    @Override
    public void onAttachedToRecyclerView(RecyclerView recyclerView) {
        super.onAttachedToRecyclerView(recyclerView);
        this.recyclerView = recyclerView;
    }

    @Override
    public void onDetachedFromRecyclerView(RecyclerView recyclerView) {
        super.onDetachedFromRecyclerView(recyclerView);
        this.recyclerView = null;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        LayoutInflater layoutInflater = LayoutInflater.from(context);
        View inflateView = null;
        switch (viewType) {
            // recipe_info	菜谱
            case 1:
                inflateView = layoutInflater.inflate(R.layout.fragment_find_item_recipe_info, parent, false);
                break;
            // shicai_info	食材
            case 2:
                inflateView = layoutInflater.inflate(R.layout.fragment_find_item_empty, parent, false);
                break;
            // huodong	活动
            case 3:
                inflateView = layoutInflater.inflate(R.layout.fragment_find_item_huodong, parent, false);
                break;
            // goods_info	商品
            case 4:
                inflateView = layoutInflater.inflate(R.layout.fragment_find_item_goods_info, parent, false);
                break;
            // article_info	阅读
            case 5:
                inflateView = layoutInflater.inflate(R.layout.fragment_find_item_article_info, parent, false);
                break;
            // gongyi_info	工艺
            case 6:
                inflateView = layoutInflater.inflate(R.layout.fragment_find_item_empty, parent, false);
                break;
            // caidan_info	菜单
            case 7:
                inflateView = layoutInflater.inflate(R.layout.fragment_find_item_caidan_info, parent, false);
                break;
            // TODO :没捉到
            case 8:
                inflateView = layoutInflater.inflate(R.layout.fragment_find_item_empty, parent, false);
                break;
            // zt_info	专题
            case 9:
                inflateView = layoutInflater.inflate(R.layout.fragment_find_item_empty, parent, false);
                break;
            // TODO :没捉到
            case 10:
                inflateView = layoutInflater.inflate(R.layout.fragment_find_item_empty, parent, false);
                break;
            // topic_info	主题
            case 11:
                inflateView = layoutInflater.inflate(R.layout.fragment_find_item_empty, parent, false);
                break;
            // TODO :没捉到
            case 12:
                inflateView = layoutInflater.inflate(R.layout.fragment_find_item_empty, parent, false);
                break;
            // TODO :没捉到
            case 13:
                inflateView = layoutInflater.inflate(R.layout.fragment_find_item_empty, parent, false);
                break;
            // TODO :没捉到
            case 14:
                inflateView = layoutInflater.inflate(R.layout.fragment_find_item_empty, parent, false);
                break;
            // duanzi_info	段子
            case 15:
                inflateView = layoutInflater.inflate(R.layout.fragment_find_item_duanzi_info, parent, false);
                break;
            // wechat_info	微信
            case 16:
                inflateView = layoutInflater.inflate(R.layout.fragment_find_item_empty, parent, false);
                break;
            // 默认加载空布局
            default:
                inflateView = layoutInflater.inflate(R.layout.fragment_find_item_empty, parent, false);
                break;
        }
        ViewHolder viewHolder = new ViewHolder(inflateView);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(final ViewHolder holder, int position) {
        holder.initView(holder.rootView);
        FindEntry.DiscoveryItemEntry item = list.get(position);
        int viewType = getItemViewType(position);
        switch (viewType) {
            // recipe_info	菜谱
            case 1:
                FindEntry.DiscoveryItemEntry.Recipe_info recipe_info = item.getRecipe_info();
                if (recipe_info != null) {
                    String titlepic = recipe_info.getTitlepic();
                    if (!TextUtils.isEmpty(titlepic)) {
                        Uri titlepicUri = Uri.parse(titlepic);
                        holder.iv_find_recipe_info_titlepic.setImageURI(titlepicUri);
                    }
                    ViewUtils.setTextView(holder.tv_find_recipe_info_title, item.getTitle());
                    ViewUtils.setTextView(holder.tv_find_recipe_info_tag, item.getTag());
                    ViewUtils.setTextView(holder.tv_find_recipe_info_contentTitle, recipe_info.getTitle());
                    ViewUtils.setTextView(holder.tv_find_recipe_info_makeTime, recipe_info.getMake_time());
                    ViewUtils.setTextView(holder.tv_find_recipe_info_kouwei, recipe_info.getKouwei());
                    try {
                        String rate = recipe_info.getRate();
                        int numStar = Integer.parseInt(rate);
                        holder.rating_find_recipe_info.setNumStars(numStar);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
                break;
            // shicai_info	食材
            case 2:

                break;
            // huodong	活动
            case 3:
                FindEntry.DiscoveryItemEntry.Huodong huodong = item.getHuodong();
                if (huodong != null) {
                    FindEntry.DiscoveryItemEntry.Huodong.ImgEntry img = huodong.getImg();
                    String smallImg = img.getSmall();
                    if (!TextUtils.isEmpty(smallImg)) {
                        Uri smallImgUri = Uri.parse(smallImg);
                        holder.iv_find_huodong_info_img.setImageURI(smallImgUri);
                    }

                    ViewUtils.setTextView(holder.tv_find_huodong_info_title, item.getTitle());
                    ViewUtils.setTextView(holder.tv_find_huodong_info_tag, item.getTag());
                }

                break;
            // goods_info	商品
            case 4:
                TextView tv_find_goods_info_saleNum;
                FindEntry.DiscoveryItemEntry.Goods_info goods_info = item.getGoods_info();
                if (goods_info != null) {
                    String image = goods_info.getImage();
                    if (!TextUtils.isEmpty(image)) {
                        Uri imageUri = Uri.parse(image);
                        holder.iv_find_goods_info_image.setImageURI(imageUri);
                    }
                    ViewUtils.setTextView(holder.tv_find_goods_info_title, item.getTitle());
                    ViewUtils.setTextView(holder.tv_find_goods_info_tag, item.getTag());
                    ViewUtils.setTextView(holder.tv_find_goods_info_contentTitle, goods_info.getTitle());

                    // 商品价格有个价格和一个市场价格(中间有一个横线)
                    String price = goods_info.getPrice();
                    if (!TextUtils.isEmpty(price)) {
                        holder.tv_find_goods_info_priceAndMarket_price.append(price);
                    }
                    String market_price = goods_info.getMarket_price();
                    if (!TextUtils.isEmpty(market_price)) {
                        holder.tv_find_goods_info_priceAndMarket_price.append("/");
                        SpannableString marketPrice = SpannableString.valueOf(market_price);
                        marketPrice.setSpan(new StrikethroughSpan(), 0, market_price.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
                        holder.tv_find_goods_info_priceAndMarket_price.append(marketPrice);
                    }

                    ViewUtils.setTextView(holder.tv_find_goods_info_saleNum, goods_info.getSale_num());
                }
                break;
            // article_info	阅读
            case 5:
                FindEntry.DiscoveryItemEntry.Article_info article_info = item.getArticle_info();
                if (article_info != null) {
                    String titlepic = article_info.getTitlepic();
                    if (!TextUtils.isEmpty(titlepic)) {
                        Uri titlepicUri = Uri.parse(titlepic);
                        holder.iv_find_article_info_titlepic.setImageURI(titlepicUri);
                    }

                    ViewUtils.setTextView(holder.tv_find_article_info_title, item.getTitle());
                    ViewUtils.setTextView(holder.tv_find_article_info_tag, item.getTag());
                    ViewUtils.setTextView(holder.tv_find_article_info_contentTitle, article_info.getTitle());
                    ViewUtils.setTextView(holder.tv_find_article_info_smalltext, article_info.getSmalltext());
                }

                break;
            // gongyi_info	工艺
            case 6:
                break;
            // caidan_info	菜单
            case 7:
                final FindEntry.DiscoveryItemEntry.Caidan_info caidan_info = item.getCaidan_info();
                if (caidan_info != null) {
                    ViewUtils.setTextView(holder.tv_find_caidan_info_caidanName, caidan_info.getCaidan_name());
                    ViewUtils.setTextView(holder.tv_find_caidan_info_tag, item.getTag());

                    List<String> photos = caidan_info.getPhotos();
                    String img1 = null;
                    String img2 = null;
                    String img3 = null;
                    switch (photos.size()) {
                        case 3:
                            img3 = photos.get(2);
                            if (!TextUtils.isEmpty(img3)) {
                                Uri img3Uri = Uri.parse(img3);
                                holder.iv_find_caidan_info_img3.setImageURI(img3Uri);
                            }
                        case 2:
                            img2 = photos.get(1);
                            if (!TextUtils.isEmpty(img2)) {
                                Uri img2Uri = Uri.parse(img1);
                                holder.iv_find_caidan_info_img2.setImageURI(img2Uri);
                            }
                        case 1:
                            img1 = photos.get(0);
                            if (!TextUtils.isEmpty(img1)) {
                                Uri img1Uri = Uri.parse(img1);
                                holder.iv_find_caidan_info_img1.setImageURI(img1Uri);
                            }
                        case 0:
                        default:
                            break;
                    }
                }
                break;
            // TODO :没捉到
            case 8:
                break;
            // zt_info	专题
            case 9:
                break;
            // TODO :没捉到
            case 10:
                break;
            // topic_info	主题
            case 11:
                break;
            // TODO :没捉到
            case 12:
                break;
            // TODO :没捉到
            case 13:
                break;
            // TODO :没捉到
            case 14:
                break;
            // duanzi_info	段子
            case 15:
                // TODO 视频还没做
                // SurfaceView surface_find_duanzi_info_vurl;

                FindEntry.DiscoveryItemEntry.Duanzi_info duanzi_info = item.getDuanzi_info();
                if (duanzi_info != null) {
                    FindEntry.DiscoveryItemEntry.Duanzi_info.User_info user_info = duanzi_info.getUser_info();
                    final FindEntry.DiscoveryItemEntry.Duanzi_info.Video_list video_list = duanzi_info.getVideo_list();

                    String avatar = user_info.getAvatar();
                    if (!TextUtils.isEmpty(avatar)) {
                        Uri avatarUri = Uri.parse(avatar);
                        holder.iv_find_duanzi_info_avatar.setImageURI(avatarUri);
                    }
                    String img_video = video_list.getImg_video();
                    if (!TextUtils.isEmpty(img_video)) {
                        Uri imgVideoUri = Uri.parse(img_video);
                        holder.iv_find_duanzi_info_imgVideo.setImageURI(imgVideoUri);
                    }
                    ViewUtils.setTextView(holder.tv_find_duanzi_info_userName, user_info.getUser_name());
                    ViewUtils.setTextView(holder.tv_find_duanzi_info_time, video_list.getTime());
                    ViewUtils.setTextView(holder.tv_find_duanzi_info_title, duanzi_info.getContent());
                    ViewUtils.setTextView(holder.tv_find_duanzi_info_playTimes, video_list.getPlay_times());

                    holder.layout_find_duanzi_info_video.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            Toast.makeText(context, "播放/暂停", Toast.LENGTH_SHORT).show();
                            holder.layout_find_duanzi_info_video.setVisibility(View.INVISIBLE);
                            holder.vv_find_duanzi_info_videourl.setVisibility(View.VISIBLE);
                            //holder.ll_find_duanzi_bofang_container.setVisibility(View.VISIBLE);
                            //holder.lpb_find_duanzi_bofang_video.setProgress(0);
                            //holder.lpb_find_duanzi_bofang_video.setMax((3*60+17)*1000);
                            //MediaPalyer 控制器 目前的版本是失败的
                            /*new Timer().schedule(new TimerTask() {
                                @Override
                                public void run() {
                                    holder.lpb_find_duanzi_bofang_video.setProgress(holder.lpb_find_duanzi_bofang_video.getProgress()+1000);
                                }
                            },1000,1000);
                            holder.vv_find_duanzi_info_videourl.setOnTouchListener(new View.OnTouchListener() {
                                @Override
                                public boolean onTouch(View v, MotionEvent event) {
                                    holder.ll_find_duanzi_bofang_container.setVisibility(View.VISIBLE);
                                    return false;
                                }
                            });
                             holder.iv_find_duanzi_bofang_max.setOnClickListener(new View.OnClickListener() {
                                @Override
                                public void onClick(View v) {
                                    Toast.makeText(context,"放大",Toast.LENGTH_SHORT).show();
                                }
                            });*/
                            holder.vv_find_duanzi_info_videourl.setVideoURI(Uri.parse(video_list.getVurl()));
                            holder.vv_find_duanzi_info_videourl.requestFocus();
                            holder.vv_find_duanzi_info_videourl.start();
                        }
                    });
                }
                break;
            // wechat_info	微信
            case 16:
                break;
            // 默认加载空布局
            default:
                break;
        }
    }

    @Override
    public int getItemCount() {
        return list == null ? 0 : list.size();
    }

    public void add(FindEntry.DiscoveryItemEntry item) {
        list.add(item);
        notifyDataSetChanged();
    }

    public void addAll(List<FindEntry.DiscoveryItemEntry> items) {
        list.addAll(items);
        notifyDataSetChanged();
    }

    public void clear() {
        list.clear();
        notifyDataSetChanged();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        View rootView;

        // 1 recipe_info	菜谱
        TextView tv_find_recipe_info_title;
        TextView tv_find_recipe_info_tag;
        SimpleDraweeView iv_find_recipe_info_titlepic;
        TextView tv_find_recipe_info_contentTitle;
        RatingBar rating_find_recipe_info;
        TextView tv_find_recipe_info_makeTime;
        TextView tv_find_recipe_info_kouwei;

        //TODO
        // 2 shicai_info	食材

        // 3 huodong	    活动
        TextView tv_find_huodong_info_title;
        TextView tv_find_huodong_info_tag;
        SimpleDraweeView iv_find_huodong_info_img;

        // 4 goods_info	    商品
        TextView tv_find_goods_info_title;
        TextView tv_find_goods_info_tag;
        SimpleDraweeView iv_find_goods_info_image;
        TextView tv_find_goods_info_contentTitle;
        TextView tv_find_goods_info_priceAndMarket_price;
        TextView tv_find_goods_info_saleNum;

        // 5 article_info	阅读
        TextView tv_find_article_info_title;
        TextView tv_find_article_info_tag;
        SimpleDraweeView iv_find_article_info_titlepic;
        TextView tv_find_article_info_contentTitle;
        TextView tv_find_article_info_smalltext;

        //TODO
        // 6 gongyi_info	工艺

        // 7 caidan_info	菜单
        TextView tv_find_caidan_info_caidanName;
        TextView tv_find_caidan_info_tag;
        SimpleDraweeView iv_find_caidan_info_img1;
        SimpleDraweeView iv_find_caidan_info_img2;
        SimpleDraweeView iv_find_caidan_info_img3;

        //TODO
        // 8

        //TODO
        // 9 zt_info	    专题

        //TODO
        // 10

        //TODO
        // 11 topic_info	主题
        //TODO
        // 12

        //TODO
        // 13

        //TODO
        // 14

        // 15 duanzi_info	段子
        SimpleDraweeView iv_find_duanzi_info_avatar;
        TextView tv_find_duanzi_info_userName;
        TextView tv_find_duanzi_info_time;
        TextView tv_find_duanzi_info_title;
        SurfaceView surface_find_duanzi_info_vurl;
        View layout_find_duanzi_info_video;
        SimpleDraweeView iv_find_duanzi_info_imgVideo;
        TextView tv_find_duanzi_info_playTimes;
        Find_Video_View vv_find_duanzi_info_videourl;
        LinearLayout ll_find_duanzi_bofang_container;
        ImageView iv_find_duanzi_bofang_max;
        ProgressBar lpb_find_duanzi_bofang_video;

        // 16 wechat_info	微信

        public ViewHolder(View itemView) {
            super(itemView);
            rootView = itemView;
        }

        public void initView(View rootView) {

            int viewType = getItemViewType();
            switch (viewType) {
                // recipe_info	菜谱
                case 1:
                    tv_find_recipe_info_title = (TextView) rootView.findViewById(R.id.tv_find_recipe_info_title);
                    tv_find_recipe_info_tag = (TextView) rootView.findViewById(R.id.tv_find_recipe_info_tag);
                    iv_find_recipe_info_titlepic = (SimpleDraweeView) rootView.findViewById(R.id.iv_find_recipe_info_titlepic);
                    tv_find_recipe_info_contentTitle = (TextView) rootView.findViewById(R.id.tv_find_recipe_info_contentTitle);
                    rating_find_recipe_info = (RatingBar) rootView.findViewById(R.id.rating_find_recipe_info);
                    tv_find_recipe_info_makeTime = (TextView) rootView.findViewById(R.id.tv_find_recipe_info_makeTime);
                    tv_find_recipe_info_kouwei = (TextView) rootView.findViewById(R.id.tv_find_recipe_info_kouwei);
                    break;
                // shicai_info	食材
                case 2:

                    break;
                // huodong	活动
                case 3:
                    tv_find_huodong_info_title = (TextView) rootView.findViewById(R.id.tv_find_huodong_info_title);
                    tv_find_huodong_info_tag = (TextView) rootView.findViewById(R.id.tv_find_huodong_info_tag);
                    iv_find_huodong_info_img = (SimpleDraweeView) rootView.findViewById(R.id.iv_find_huodong_info_img);
                    break;
                // goods_info	商品
                case 4:
                    tv_find_goods_info_title = (TextView) rootView.findViewById(R.id.tv_find_goods_info_title);
                    tv_find_goods_info_tag = (TextView) rootView.findViewById(R.id.tv_find_goods_info_tag);
                    iv_find_goods_info_image = (SimpleDraweeView) rootView.findViewById(R.id.iv_find_goods_info_image);
                    tv_find_goods_info_contentTitle = (TextView) rootView.findViewById(R.id.tv_find_goods_info_contentTitle);
                    tv_find_goods_info_priceAndMarket_price = (TextView) rootView.findViewById(R.id.tv_find_goods_info_priceAndMarket_price);
                    tv_find_goods_info_saleNum = (TextView) rootView.findViewById(R.id.tv_find_goods_info_saleNum);
                    break;
                // article_info	阅读
                case 5:
                    tv_find_article_info_title = (TextView) rootView.findViewById(R.id.tv_find_article_info_title);
                    tv_find_article_info_tag = (TextView) rootView.findViewById(R.id.tv_find_article_info_tag);
                    iv_find_article_info_titlepic = (SimpleDraweeView) rootView.findViewById(R.id.iv_find_article_info_titlepic);
                    tv_find_article_info_contentTitle = (TextView) rootView.findViewById(R.id.tv_find_article_info_contentTitle);
                    tv_find_article_info_smalltext = (TextView) rootView.findViewById(R.id.tv_find_article_info_smalltext);
                    break;
                // gongyi_info	工艺
                case 6:
                    break;
                // caidan_info	菜单
                case 7:
                    tv_find_caidan_info_caidanName = (TextView) rootView.findViewById(R.id.tv_find_caidan_info_caidanName);
                    tv_find_caidan_info_tag = (TextView) rootView.findViewById(R.id.tv_find_caidan_info_tag);
                    iv_find_caidan_info_img1 = (SimpleDraweeView) rootView.findViewById(R.id.iv_find_caidan_info_img1);
                    iv_find_caidan_info_img2 = (SimpleDraweeView) rootView.findViewById(R.id.iv_find_caidan_info_img2);
                    iv_find_caidan_info_img3 = (SimpleDraweeView) rootView.findViewById(R.id.iv_find_caidan_info_img3);
                    break;
                // TODO :没捉到
                case 8:
                    break;
                // zt_info	专题
                case 9:
                    break;
                // TODO :没捉到
                case 10:
                    break;
                // topic_info	主题
                case 11:
                    break;
                // TODO :没捉到
                case 12:
                    break;
                // TODO :没捉到
                case 13:
                    break;
                // TODO :没捉到
                case 14:
                    break;
                // duanzi_info	段子
                case 15:
                    iv_find_duanzi_info_avatar = (SimpleDraweeView) rootView.findViewById(R.id.iv_find_duanzi_info_avatar);
                    tv_find_duanzi_info_userName = (TextView) rootView.findViewById(R.id.tv_find_duanzi_info_userName);
                    tv_find_duanzi_info_time = (TextView) rootView.findViewById(R.id.tv_find_duanzi_info_time);
                    tv_find_duanzi_info_title = (TextView) rootView.findViewById(R.id.tv_find_duanzi_info_title);
                    //surface_find_duanzi_info_vurl = (SurfaceView) rootView.findViewById(R.id.surface_find_duanzi_info_vurl);
                    layout_find_duanzi_info_video = rootView.findViewById(R.id.layout_find_duanzi_info_video);
                    iv_find_duanzi_info_imgVideo = (SimpleDraweeView) rootView.findViewById(R.id.iv_find_duanzi_info_imgVideo);
                    tv_find_duanzi_info_playTimes = (TextView) rootView.findViewById(R.id.tv_find_duanzi_info_playTimes);
                    vv_find_duanzi_info_videourl = (Find_Video_View) rootView.findViewById(R.id.surface_find_duanzi_info_vurl);
                   /* ll_find_duanzi_bofang_container = ((LinearLayout) rootView.findViewById(R.id.ll_find_duanzi_bofang_container));
                    lpb_find_duanzi_bofang_video = ((ProgressBar) rootView.findViewById(R.id.lpb_find_duanzi_bofang_video));
                    iv_find_duanzi_bofang_max = (ImageView) rootView.findViewById(R.id.iv_find_duanzi_bofang_max);*/
                    break;
                // wechat_info	微信
                case 16:
                    break;
                // 默认加载空布局
                default:
                    break;
            }
        }
    }
}
